package com.alibaba.aone.casestudy.module.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.CasestudyAnalysisParticipant;
import com.alibaba.aone.casestudy.service.ActionService;
import com.alibaba.aone.casestudy.service.CasestudyService;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.common.lang.StringUtil;

public class CasestudyProcessAction {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private FailureService failureService;
	@Autowired
	private CasestudyService casestudyService;
	@Autowired
	private ActionService actionService;

	public void doEdit(@Param("csno") String csNumber, Context context) {

		if (StringUtil.isNotEmpty(csNumber)) {
			//casestudy info
			String analysisDate = request.getParameter("analysisDate");
			String initiator = request.getParameter("initiator");
			String participants = request.getParameter("participants");
			String checker = request.getParameter("checker");
			
			//failure info
			String failureNumber = request.getParameter("failureno");
			String failureTitle = request.getParameter("failureTitle");
			String discoverMethod = request.getParameter("discoverMethod");
			String reasonClear = request.getParameter("reasonClear");
			String score = request.getParameter("score");
			
			System.out.println(request.getParameter("participants"));
			System.out.println(request.getParameter("nok"));
			String[] descs = request.getParameterValues("desc");
			String[] chargemen = request.getParameterValues("chargeman");
			String[] followers = request.getParameterValues("follower");
			String[] latestCompleteTimes = request
					.getParameterValues("latestCompleteTime");
			
			
			String influence = request.getParameter("influence");
			String solution = request.getParameter("solution");
			String reasonAnalysis = request.getParameter("reasonAnalysis");
			
			//monitor info
			String avoidMethod = request.getParameter("avoidMethod");
			
			
			/*
			 * for(String desc:descs){ System.out.println(desc); }
			 */
			for (int i = 0; i < descs.length; i++) {
				System.out.println(descs[i] + " : " + chargemen[i] + " : "
						+ followers[i] + " : " + latestCompleteTimes[i]);
			}
			
			//update casestudy info
			List<CasestudyAnalysisParticipant> analysisPartis = parseAnalysisParti(participants, csNumber);
			casestudyService.updateAnalysisPartis(analysisPartis, csNumber);
		}
		
	}
	
	public List<CasestudyAnalysisParticipant> parseAnalysisParti(String participants, String casestudyNumber){
		ArrayList<CasestudyAnalysisParticipant> list = new ArrayList<CasestudyAnalysisParticipant>();
		String[] parts = participants.split(",");
		for(String part:parts){
			String[] person = part.split(":");
			String accountId = person[0];
			String name = person[1];
			CasestudyAnalysisParticipant analysisPer = new CasestudyAnalysisParticipant();
			analysisPer.setCasestudyNumber(casestudyNumber);
			analysisPer.setAccountId(accountId);
			analysisPer.setName(name);
			list.add(analysisPer);
		}
		return list;
	}
	
}